package com.zwk.gulimall.product.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zwk.gulimall.product.entity.CategoryEntity;
import com.zwk.gulimall.product.service.CategoryService;
import com.zwk.common.utils.PageUtils;
import com.zwk.common.utils.R;



/**
 * 商品三级分类
 *
 * @author zhuwenkang
 * @email zhuwenkang@gmail.com
 * @date 2022-12-30 22:46:58
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list/tree")
    //@RequiresPermissions("product:category:list")
    public R listWithTree(@RequestParam Map<String, Object> params){
        System.out.println();
        List<CategoryEntity> categoryEntities = categoryService.listWithTree();
        return R.ok().put("data", categoryEntities);
    }

    /**
     * 获取当前节点到根节点的链条
     * @param catId:当前节点
     * @return:一个包含list集合的R对象
     */
    @RequestMapping("/link/{catId}")
    public R getLink(@PathVariable Long catId){
        List<CategoryEntity> catelogPath =categoryService.findCatelogPath(catId);
        return R.ok().put("data",catelogPath);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);
        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateDetail(category);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update/sort")
    //@RequiresPermissions("product:category:update")
    public R updateSort(@RequestBody List<CategoryEntity> categories){
        categoryService.updateBatchById(categories);
        return R.ok();
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds){
        //TODO 1.检查菜单是否被引用
		categoryService.removeByIds(Arrays.asList(catIds));
        return R.ok();
    }

}
